package com.cs.framework.ui.builder.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cs.framework.common.util.ContainerUtils;
import com.cs.framework.common.util.StringUtils;
import com.cs.framework.ui.dto.CSPanelButtonDto;
import com.cs.framework.ui.dto.CSPanelLayoutDto;
import com.cs.framework.ui.dto.CSPanelProSetDto;
import com.cs.framework.ui.exception.UIExceptionHelper;
import com.cs.framework.ui.model.FieldInfo;
import com.cs.framework.ui.model.PanelInfo;
import com.cs.framework.ui.model.UIInfo;
import com.cs.framework.ui.util.UIConstant;

/**
 * 构建面板
 * 
 * @author hedongzhou
 * 
 */
public class PanelBuilder extends AbstractDbUIBuilder {

	/**
	 * 构建表单
	 */
	protected FieldBuilder fieldBuilder;

	public FieldBuilder getFieldBuilder() {
		return fieldBuilder;
	}

	/**
	 * 初始化
	 */
	public void init() {
		super.init();
		fieldBuilder = new FieldBuilder();
		fieldBuilder.setDefDao(defDao);
		fieldBuilder.init();
	}

	/**
	 * 构建面板
	 * 
	 * @param panelId
	 *            面板ID
	 * @param superPanelId
	 *            父面板ID
	 * @return
	 */
	public UIInfo build(String panelId, String superPanelId) {
		// 查询面板
		PanelInfo panel = defDao.getPanel(panelId, PanelInfo.class);
		if (panel == null) {
			UIExceptionHelper.throwUIException("UI003", panelId);
		}

		panel.setSubFieldList(new ArrayList<FieldInfo>());
		panel.setSubPanelList(new ArrayList<PanelInfo>());
		// 查询面板布局
		List<CSPanelLayoutDto> panelLayoutList = defDao.getPanelLayoutList(panelId);
		if (ContainerUtils.listIsNotEmpty(panelLayoutList)) {
			if (UIConstant.ChildType.field.equals(panel.getChildType())) {
				String tempClassName = null;
				String className = panel.getFieldClassName();

				for (CSPanelLayoutDto panelLayout : panelLayoutList) {
					// 获取字段信息
					FieldInfo field = (FieldInfo) fieldBuilder.build(panelLayout.getChildId(), panelId);

					if (StringUtils.isNotBlank(className)) {
						field.setClassName(className);
					} else if (StringUtils.isBlank(tempClassName)) {
						tempClassName = field.getClassName();
					}

					panel.getSubFieldList().add(field);
				}

				if (StringUtils.isBlank(className) && StringUtils.isNotBlank(tempClassName)) {
					panel.setFieldClassName(tempClassName);
				}
			} else {
				for (CSPanelLayoutDto panelLayout : panelLayoutList) {
					// 获取面板信息
					panel.getSubPanelList().add((PanelInfo) this.build(panelLayout.getChildId(), panelId));
				}
			}
		}

		// 查询面板按钮
		List<CSPanelButtonDto> buttonList = defDao.getPanelButtonList(panelId);
		if (ContainerUtils.listIsNotEmpty(buttonList)) {
			panel.setButtonList(buttonList);
		} else {
			panel.setButtonList(new ArrayList<CSPanelButtonDto>());
		}

		panel.setProMap(new HashMap<String, String>());
		// 查询面板属性
		List<CSPanelProSetDto> panelProSetList = null;
		if (StringUtils.isBlank(superPanelId)) {
			panelProSetList = defDao.getPanelProSetList(panel.getPanelId());
		} else {
			panelProSetList = defDao.getPanelProSetList(panel.getPanelId(), superPanelId);
		}
		if (ContainerUtils.listIsNotEmpty(panelProSetList)) {
			Map<String, String> proMap = panel.getProMap();
			for (CSPanelProSetDto panelProSet : panelProSetList) {
				// 添加属性
				proMap.put(panelProSet.getPanelPro(), panelProSet.getValue());
			}
		}

		return panel;
	}

}
